package com.steak.business.service;

import com.steak.business.client.IntegralClient;
import com.steak.business.client.OrderClient;
import com.steak.business.client.StockClient;
import com.steak.common.domain.dataobject.tcc.Integral;
import com.steak.common.domain.dataobject.tcc.Order;
import com.steak.common.domain.dataobject.tcc.Stock;
import com.steak.common.domain.dto.PlaceOrderDTO;
import com.steak.common.result.Response;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author 刘牌
 * @date 2022/3/116:22
 */
@Component
@AllArgsConstructor
public class PlaceOrderExecute {

    private final IntegralClient integralClient;
    private final StockClient stockClient;
    private final OrderClient orderClient;

    @GlobalTransactional
    public Response execute(PlaceOrderDTO placeOrderDTO) {
        //保存订单
        Order order = new Order()
                .setUserId(placeOrderDTO.getUserId())
                .setCommodityId(placeOrderDTO.getCommodityId())
                .setCount(placeOrderDTO.getCount())
                .setMoney(placeOrderDTO.getMoney());
        orderClient.saveOrder(order);
        //扣减积分
        Integral integral = new Integral().setUserId(placeOrderDTO.getUserId()).setIntegral(10);
        integralClient.increaseIntegral(integral);
        //扣减库存
        Stock stock = new Stock().setCommodityId(placeOrderDTO.getCommodityId()).setStockNum(placeOrderDTO.getCount());
        stockClient.decreaseStock(stock);
        return new Response(200,"placeOrder success",null);
    }
}
